package com.ruoyix.flowable.util;



import lombok.RequiredArgsConstructor;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.engine.TaskService;
import org.flowable.bpmn.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Component
public class FlowableUtil {


    @Autowired
    private  RuntimeService runtimeService;
    @Autowired
    private  TaskService taskService;
    @Autowired
    private  HistoryService historyService;
    @Autowired
    private  RepositoryService repositoryService;


    public static FlowableUtil flowableUtil;
    // 静态方法，用于初始化服务
    @PostConstruct
    public void init() {
        flowableUtil = this;
        flowableUtil.runtimeService = this.runtimeService;
        flowableUtil.historyService =this.historyService;
        flowableUtil.taskService =this.taskService;
        flowableUtil.repositoryService =this.repositoryService;
    }

    public static String getTaskStatus(String taskId) {
        HistoricTaskInstance historicTask = flowableUtil.historyService.createHistoricTaskInstanceQuery()
            .taskId(taskId)
            .singleResult();

        if (historicTask != null && historicTask.getEndTime() != null) {
            return "Completed";
        }

        Task task = flowableUtil.taskService.createTaskQuery().taskId(taskId).singleResult();
        if (task != null) {
            if (task.isSuspended()) {
                return "Suspended";
            } else if (task.getDelegationState() != null) {
                return "Delegated";
            } else {
                return "Active";
            }
        } else {
            return "Not Found";
        }
    }

    public static List<FlowElement> getNextNodeList(String processInstanceId, String currentTaskId) {
        List<FlowElement> nextNodes = new ArrayList<>();

        Task currentTask = flowableUtil.taskService.createTaskQuery()
            .taskId(currentTaskId)
            .singleResult();

        ProcessInstance processInstance = flowableUtil.runtimeService.createProcessInstanceQuery()
            .processInstanceId(processInstanceId)
            .singleResult();

        BpmnModel bpmnModel = flowableUtil.repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());

        FlowElement flowElement = bpmnModel.getFlowElement(currentTask.getTaskDefinitionKey());
        List<SequenceFlow> outgoingFlows = ((FlowNode) flowElement).getOutgoingFlows();

        for (SequenceFlow sequenceFlow : outgoingFlows) {
            FlowElement outFlowElement = bpmnModel.getFlowElement(sequenceFlow.getTargetRef());

            if (outFlowElement instanceof UserTask) {
                nextNodes.add(outFlowElement);
            } else if (outFlowElement instanceof ExclusiveGateway || outFlowElement instanceof ParallelGateway || outFlowElement instanceof InclusiveGateway) {
                List<FlowElement> gatewayNextNodes = getNextNodeList(processInstanceId, currentTaskId);
                nextNodes.addAll(gatewayNextNodes);
            } else if (outFlowElement instanceof EndEvent) {
                // 处理结束事件
            } else if (outFlowElement instanceof SubProcess) {
                // 处理子流程
            } else {
                // 处理其他类型的流程元素
            }
        }

        return nextNodes;
    }
}
